package com.application.lucky.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.ToastUtils;
import com.qunar.im.ui.imagepicker.util.Utils;
import com.qunar.im.ui.view.zxing.manager.QRCodeManager;

/**
 * create by liulp
 * on 2019-11-12 18:08
 */
public class ZCodeActivity extends BaseActivity {
    private TitlebarView mTitleBar;
    private TextView tv_nick;
    private ImageView iv_zcode;
    private String nick;
    private String urlInfo;

    @Override
    public void beforeInit() {
        Intent intent = getIntent();
        nick = intent.getStringExtra("nick");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_zcode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {
            }
        });

        iv_zcode = findViewById(R.id.iv_zcode);
        tv_nick = findViewById(R.id.tv_nick);
        tv_nick.setText(UserManagers.getInstance().getShowName());

        // TODO 后期上线修改前缀
        String inviteUrl = "https://app-jotojp.openinstall.io/js-test/android/980508959221823175?userId="+ UserManagers.getInstance().getUserBean().getUserId();
        Bitmap bitmap = QRCodeManager.getInstance().createQRCode(inviteUrl, Utils.dp2px(ZCodeActivity.this, 145), Utils.dp2px(ZCodeActivity.this, 145));
        iv_zcode.setImageBitmap(bitmap);

        final TextView tv_invite_userid = findViewById(R.id.tv_invite_userid);
        tv_invite_userid.setText(UserManagers.getInstance().getUserId());
        findViewById(R.id.btn_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData clipData = ClipData.newPlainText("Label", tv_invite_userid.getText().toString());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(clipData);
                ToastUtils.show("复制邀请码成功");
            }
        });
    }
}
